#!/bin/bash
# This script sets up the Apache server to access
# websm help

#check if the apache server is installed
cd /
#echo "The package is : $package"
if [ -f /usr/sbin/httpd ]
then
        #echo "Apache Package is installed....."
        #check if the httpd.conf file exists
        if [ -f /etc/httpd/httpd.conf ]
        then
		cp /etc/httpd/httpd.conf /etc/httpd/httpd.prevConf
                #echo "the config file exists"
		#check if the ServerName is already set
		hname=""
	        hname=`grep -v ^# /etc/httpd/httpd.conf | grep ServerName | cut -d ' ' -f 2`	
		if [ "$hname" != "" ]
		then 

		     #echo "Server Name exists hence needs to be changed"
		     awk '// {if ($1 ~ /^ServerName/) {printf("%s\n", hn)} else {print}}' hn="ServerName $HOSTNAME" /etc/httpd/httpd.conf > /etc/httpd/httpd.mod
		     cp /etc/httpd/httpd.mod /etc/httpd/httpd.conf
		     rm /etc/httpd/httpd.mod
		
		else	

                     # add the Server Name to the end of the file 
                     #echo "The host name is $HOSTNAME"
                     echo "ServerName $HOSTNAME" >> /etc/httpd/httpd.conf
		fi
	    fi

	    if [ ! -L /srv/www/help ]
	    then
	        #echo "Setting up the link"
	        ln -s /opt/hsc/help /srv/www/
	    fi
	
	    cd /
	    sed -e 's/-FollowSymLinks/+FollowSymLinks/g' /etc/httpd/httpd.conf > /tmp/_newHttpd.conf_
	    mv /tmp/_newHttpd.conf_ /etc/httpd/httpd.conf
	 


# ====================================================================================================
#              Access Control for Apache
#
#  Access control is implemented using Apache directives. 
#  Deny from all - denies the acces from all domains
#  Allow from all - allow the access from all domains
#
# For more detailed information on access control, please visit to http://httpd.apache.org/docs/
# ====================================================================================================


	hmcTagFinder=`grep -Fw "#HMC Configuration" /etc/httpd/httpd.conf`

	if [ "$hmcTagFinder" == "" ]
	then
	echo adding HMC specific entries to httpd.conf 
   	echo "

#HMC Configuration"  >> /etc/httpd/httpd.conf

  	/bin/sed -e 's/Options FollowSymLinks/Order allow,deny/' /etc/httpd/httpd.conf > /etc/httpd/httpd.conf.mod

       /bin/cp /etc/httpd/httpd.conf.mod /etc/httpd/httpd.conf
       /bin/rm -rf /etc/httpd/httpd.conf.mod

      /bin/sed -e 's/AllowOverride None/Deny from all/' /etc/httpd/httpd.conf > /etc/httpd/httpd.conf.mod

  /bin/cp /etc/httpd/httpd.conf.mod /etc/httpd/httpd.conf
  /bin/rm -rf /etc/httpd/httpd.conf.mod

  /bin/sed -e 's/Options Indexes FollowSymLinks/#Options Indexes FollowSymLinks/' /etc/httpd/httpd.conf > /etc/httpd/httpd.conf.mod

  /bin/cp /etc/httpd/httpd.conf.mod /etc/httpd/httpd.conf
  /bin/rm -rf /etc/httpd/httpd.conf.mod

  /bin/sed -e 's/AllowOverride/#AllowOverride/' /etc/httpd/httpd.conf > /etc/httpd/httpd.conf.mod

  /bin/cp /etc/httpd/httpd.conf.mod /etc/httpd/httpd.conf
  /bin/rm -rf /etc/httpd/httpd.conf.mod

  /bin/sed -e 's/Allow from all/Deny from all/' /etc/httpd/httpd.conf > /etc/httpd/httpd.conf.mod

  /bin/cp /etc/httpd/httpd.conf.mod /etc/httpd/httpd.conf
  /bin/rm -rf /etc/httpd/httpd.conf.mod
fi
grep "hmchttpd.conf" /etc/httpd/httpd.conf 2>/dev/null
if [ $? -ne 0 ]
then
    echo "Include /opt/hsc/data/hmchttpd.conf" >> /etc/httpd/httpd.conf
fi

grep "websmhttpd.conf" /etc/httpd/httpd.conf 2>/dev/null
if [ $? -ne 0 ]
then
    echo "Include /opt/hsc/data/websmhttpd.conf" >> /etc/httpd/httpd.conf
fi

grep "AuthName \"HMC\"" /etc/httpd/httpd.conf 2>/dev/null
if [ $? -ne 0 ]
then
   awk '// {if ($0 ~ /<Directory \"\/srv\/www\/htdocs\">/) \
           {print; \
            print "       AuthName \"HMC\""; \
            print "       AuthType Basic"; \
            print "       AuthUserFile /usr/local/etc/httpd/users"; \
            print "       require valid-user"; \
            print ""; \
            print ""; \
            print ""; \
 	    print "<Files /srv/www/htdocs/remote_client.html>"; \
	    print "    Options +FollowSymLinks +Includes +MultiViews";\
	    print "</Files>";\
           } else {print}}' /etc/httpd/httpd.conf > /tmp/httpd.mod
   mv /tmp/httpd.mod /etc/httpd/httpd.conf
fi

fi
